package cn.myframe.service.sys.impl;

import cn.myframe.dao.sys.SysUserInfoDao;
import cn.myframe.entity.sys.SysUserInfoEntity;
import cn.myframe.service.sys.SysUserInfoService;
import cn.myframe.utils.AESSecurityUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;



@Service("sysUserInfoService")
public class SysUserInfoServiceImpl implements SysUserInfoService {
	@Autowired
	private SysUserInfoDao sysUserInfoDao;
	
	@Override
	public SysUserInfoEntity queryObject(Long id) throws Exception{
	    
	    SysUserInfoEntity sysUserInfoEntity = sysUserInfoDao.queryObject(id);
	    if(sysUserInfoEntity!=null){
	        String key = "myframe.cn";
	        if(StringUtils.isNotEmpty(sysUserInfoEntity.getAppsecret())){
	            String appsecret = AESSecurityUtil.decrypt(sysUserInfoEntity.getAppsecret(), key);
	            sysUserInfoEntity.setAppsecret(appsecret);
	        }	    
	        if(StringUtils.isNotEmpty(sysUserInfoEntity.getApikey())){
	            String apikey = AESSecurityUtil.decrypt(sysUserInfoEntity.getApikey(), key);
	            sysUserInfoEntity.setApikey(apikey);
	        }	        
	    }
	    
		return sysUserInfoEntity;
	}
	
	@Override
	public List<SysUserInfoEntity> queryList(Map<String, Object> map){
		return sysUserInfoDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return sysUserInfoDao.queryTotal(map);
	}
	
	@Override
	public void save(SysUserInfoEntity sysUserInfo) throws Exception{

        String key = "myframe.cn";
        if(StringUtils.isNotEmpty(sysUserInfo.getAppsecret())){
            String appsecret = AESSecurityUtil.encrypt(sysUserInfo.getAppsecret(), key);
            sysUserInfo.setAppsecret(appsecret);
        }       
        if(StringUtils.isNotEmpty(sysUserInfo.getApikey())){
            String apikey = AESSecurityUtil.encrypt(sysUserInfo.getApikey(), key);
            sysUserInfo.setApikey(apikey);
        }
	    
		sysUserInfoDao.save(sysUserInfo);
	}
	
	@Override
	public void update(SysUserInfoEntity sysUserInfo) throws Exception{
        String key = "myframe.cn";
        if(StringUtils.isNotEmpty(sysUserInfo.getAppsecret())){
            String appsecret = AESSecurityUtil.encrypt(sysUserInfo.getAppsecret(), key);
            sysUserInfo.setAppsecret(appsecret);
        }       
        if(StringUtils.isNotEmpty(sysUserInfo.getApikey())){
            String apikey = AESSecurityUtil.encrypt(sysUserInfo.getApikey(), key);
            sysUserInfo.setApikey(apikey);
        }	    
	    
		sysUserInfoDao.update(sysUserInfo);
	}
	
	@Override
	public void delete(Long id){
		sysUserInfoDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		sysUserInfoDao.deleteBatch(ids);
	}

    @Override
    public List<SysUserInfoEntity> queryByAppId(String appid) throws Exception {
        List<SysUserInfoEntity> sysUserInfoList = sysUserInfoDao.queryByAppId(appid);
        if(sysUserInfoList != null && sysUserInfoList.size() > 0){
            String key = "myframe.cn";
            for(SysUserInfoEntity sysUserInfoEntity : sysUserInfoList){
                if(StringUtils.isNotEmpty(sysUserInfoEntity.getAppsecret())){
                    String appsecret = AESSecurityUtil.decrypt(sysUserInfoEntity.getAppsecret(), key);
                    sysUserInfoEntity.setAppsecret(appsecret);
                }       
                if(StringUtils.isNotEmpty(sysUserInfoEntity.getApikey())){
                    String apikey = AESSecurityUtil.decrypt(sysUserInfoEntity.getApikey(), key);
                    sysUserInfoEntity.setApikey(apikey);
                }            
            }
            
        }
        
        
        return sysUserInfoList;
    }

	@Override
	public boolean checkExist(String id, String appid) {
		return sysUserInfoDao.checkExist(id, appid) <= 0;
	}

	@Override
	public SysUserInfoEntity queryByCompanyNo(Long companyNo) {
		return sysUserInfoDao.queryByCompanyNo(companyNo);
	}

	@Override
	public List<SysUserInfoEntity> queryLists() {
		return sysUserInfoDao.queryLists();
	}

	@Override
	public void updateInfo(SysUserInfoEntity sysUserInfoEntity) {
		sysUserInfoDao.update(sysUserInfoEntity);
	}

	@Override
	public SysUserInfoEntity selectById(Long id) {
		return sysUserInfoDao.selectById(id);
	}

}
